<?php
/**
 * Created by JetBrains PhpStorm.
 * User: akrug
 * Date: 19.06.12
 * Time: 15:50
 * To change this template use File | Settings | File Templates.
 */
class Backend_Model_DbTable_Busdepartment extends UcsMvc_AbstractDbTable
{
    protected $_name = 'bus_department';

    /**
     * gibt alle unternehmensbereiche eines bestimmten unternehmens in einer bestimmten sprache aus
     *
     * @param $companyNid
     * @param $language
     *
     * @return array
     */
    public function getDepartments($companyNid, $languageNid){
        $companies = new Backend_Model_DbTable_Buscompany();
        $languages = new Backend_Model_DbTable_Language();

        $result = false;
        //pruefen, ob languageCId und companyNid fuer die ausgabe validen elementen gehoeren
        if($companies->isValid($companyNid) && $languages->isValid($languageNid)){
            $select = $this->getAdapter()
                            ->select()
                            ->from($this->_name)
                            ->join('bus_dep2lang', $this->_name.'.'.$this->_primary[1].' = bus_dep2lang.bus_department_nid')
                            ->where($this->_name.'.bus_company_nid = ?', $companyNid) //unternehmen
                            ->where($this->_name.'.bus_department_hidden = 0')
                            ->where($this->_name.'.bus_department_deleted = 0')
                            ->where('bus_dep2lang.language_nid = ?', $languageNid);//sprache

            $result = $this->getAdapter()->fetchAll($select);
        }

        return $result;
    }

    /**
     * @param $companyNid
     * @param $languageNid
     *
     * @return array
     *
     * erzeugt departmentliste mit videokategorien und bildkategorien und den elementen, die dazu gehoeren
     */
    public function getDepartmentMedia($companyNid, $languageNid){
        $videoCatTable = new Backend_Model_DbTable_VidCategory();

        $departments = self::getDepartments($companyNid, $languageNid);

        foreach($departments as $departmentIndex => $department){
            //$departments[$departmentIndex]['pictureCats'] = $this->getDepartmentPicturesCat();
            $departments[$departmentIndex]['videoCats'] = $videoCatTable->getVideoCatsComplete($department[$this->_primary[1]], $languageNid);
        }

        return $departments;
    }

    /**
     * fuegt eine neue zeile in bus_department ein und eine neue zeile in bus_dep2lang
     *
     * @param $params
     * @param $languageCid
     *
     * @return integer
     *
     * overrides addRow function in abstractDbTable
     */
    public function addRow($params, $companyNid, $languageNid){
        $cols = $this->_getCols();

        $params['bus_company_nid'] = $companyNid;
        $params['language_nid']    = $languageNid;

        //fuegt das erstelldatum hinzu, falls das Attribut vorhanden ist
        if(in_array($this->_name.'_created', $cols)){
            $params[$this->_name.'_created'] = date('Y-m-d H:i:s');
        }

        //erstelle tempparams weil params noch gebraucht wird
        $paramsTemp = $params;
        //entfernt attribute aus uebergebenem array, die kein datenbankattribute sind
        foreach($paramsTemp as $pIndex => $p){
            if(!in_array($pIndex, $cols)){
                unset($paramsTemp[$pIndex]);
            }
        }

        $id = $this->insert($paramsTemp);

        //fuege referenzid hinzu
        $params[$this->_primary[1]] = $id;

        $busDep2Lang = new Backend_Model_DbTable_BusDep2Lang();
        $busDep2Lang->addRow($params);
    }

    /**
     * @param array $params
     * @param null  $id
     *
     * @return bool
     */
    public function editRow(array $params, $id = null){
        $cols    = $this->_getCols();

        if(isset($params['id'])){
            $id = $params['id'];
        }

        $paramsTemp = $params;
        if(!$this->deleted($id)){
            foreach($paramsTemp as $pIndex => $p){
                if(!in_array($pIndex, $cols)){
                    unset($paramsTemp[$pIndex]);
                }
            }
            $busDep2LangTable = new Backend_Model_DbTable_BusDep2Lang();
            $busDep2LangTable->editRow($params, $id);

            return true;
        }

        return false;
    }

    /**
     * @return array - one defined Category
     */
    public function getRow($id){
        if(!self::deleted($id)){
            $select = $this->getAdapter()
                ->select()
                ->from($this->_name)
                ->join('bus_dep2lang', $this->_name.'.'.$this->_primary[1].' = bus_dep2lang.'.$this->_primary[1])
                ->where( $this->_name.'.'.$this->_primary[1].' = '.$id);
            $result = $this->getAdapter()->fetchAll($select);
            return $result[0];
        }

        return false;
    }
}